<?php
require_once 'ArticleInfoSaver.php';
require_once 'ArticleInfoReader.php';
require_once 'Util.php';

class ArticleInfoManager {
    var $root;
    var $saver;
    var $logger;
    var $lines;

    var $COLUMN_PATH            = 0;
    var $COLUMN_TITLE           = 1;
    var $COLUMN_PAGETITLE       = 2;
    var $COLUMN_PUBDATE         = 3;
    var $COLUMN_AUTHOR          = 4;
    var $COLUMN_THEME           = 5;
    var $COLUMN_SUBTHEME        = 6;
    var $COLUMN_MAINIMAGE       = 7;
    var $COLUMN_INTRO           = 8;

    function ArticleInfoManager($root) {
        $this->root = $root;
        $this->saver = new ArticleInfoSaver($this->root);
        $this->logger = new Logger($root, "ArticleInfoManager");
    }

    /**
     * TODO: this function needs write access to articles.
     * Move it to protected area!
     *
     * Adds article info to the articles info table file.
     * @param path the path to article file
     */
    function storeArticleInfo($path) {
        $articleInfoReader = new ArticleInfoReader();
        $articleInfoReader->read($path);
        $articleInfoSaver = new ArticleInfoSaver($this->root);
        $articleInfoSaver->save($articleInfoReader->articleInfoArr, false);
    }

    function getArticleInfo($path) {
        // open articlesInfo.txt
        $lines = $this->readBaseFile();

        // find record with needed path
        for ($i=0; $i < count($lines); $i++) {
            if (strcmp($path, $this->getPath($lines[$i])) == 0) {
                return $this->parseArticleInfo($lines[$i]);
            }
        }
        $this->logger->applogSevere("Can not find article info for the path '$path'");
    }

    function getAllArticleInfos() {
        $articleInfos = array();

        // open articlesInfo.txt
        $lines = $this->readBaseFile();

        // find record with needed path
        for ($i=0; $i < count($lines); $i++) {
            $articleInfos[$i] = $this->parseArticleInfo($lines[$i]);
        }

        $this->logger->applogInfo("getAllArticleInfos(): found ".count($articleInfos)." aticles");
        return $articleInfos;
    }

    function getArticleInfosByTheme($theme) {
        $articleInfos = array();
        $index = 0;

        // open articlesInfo.txt
        $lines = $this->readBaseFile();

        // find record with needed path
        for ($i=0; $i < count($lines); $i++) {
            if (strcmp($theme, $this->getTheme($lines[$i])) == 0) {
                $articleInfos[$index++] = $this->parseArticleInfo($lines[$i]);
            }
        }
        $this->logger->applogInfo("getArticleInfosByTheme(): found ".count($articleInfos)." aticles fot theme '$theme'");
        return $articleInfos;
    }

    function getThemes() {
        $themes = array();
        $index = 0;

        $lines = $this->readBaseFile();
        foreach ($lines as $line) {
            $aTheme = $this->getTheme($line);
            if(!containsStr($themes, $aTheme)) {
                $themes[$index++] = $aTheme;
            }
        }

        // sort themes alphabetically
        sort(&$themes);

        $this->logger->applogInfo("getThemes(): found ".count($themes)." themes");
        return $themes;
    }

    private function readBaseFile() {
        if (!$this->lines) {
            $this->lines = file($this->saver->articlesInfoFile);
            if (!$this->lines || count($this->lines) <= 0) {
                $msg = "can not read articles info file '$this->saver->articlesInfoFile'";
                $this->logger->applogSevereDie($msg);
            }
        }
        return $this->lines;
    }

    private function getPath($line) {
        $lineWithoutN = cutNewLine($line);
        $columns = explode($this->saver->columnDelimeter, $lineWithoutN);
        return $columns[$this->COLUMN_PATH];
    }
    private function getTheme($line) {
        $lineWithoutN = cutNewLine($line);
        $columns = explode($this->saver->columnDelimeter, $lineWithoutN);
        return $columns[$this->COLUMN_THEME];
    }
    private function parseArticleInfo($line) {
        $lineWithoutN = cutNewLine($line);
        $columns = explode($this->saver->columnDelimeter, $lineWithoutN);
        
        $articleInfo = new ArticleInfo();

        $articleInfo->link = $columns[$this->COLUMN_PATH];
        $articleInfo->title = $columns[$this->COLUMN_TITLE];
        $articleInfo->pageTitle = $columns[$this->COLUMN_PAGETITLE];
        $articleInfo->pubdate = $columns[$this->COLUMN_PUBDATE];
        $articleInfo->author = $columns[$this->COLUMN_AUTHOR];
        $articleInfo->theme = $columns[$this->COLUMN_THEME];
        $articleInfo->subtheme = $columns[$this->COLUMN_SUBTHEME];
        $articleInfo->mainImage = $columns[$this->COLUMN_MAINIMAGE];
        $articleInfo->intro = $columns[$this->COLUMN_INTRO];

        $this->logger->applogInfo("parseArticleInfo: ".$articleInfo->toString() . " from line '$lineWithoutN'");
        return $articleInfo;
    }
}

?>
